package com.example.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class HelloController {

    // 原有方法
    @GetMapping("/hello")
    @ResponseBody
    public String hello() {
        return "Hello, World!";
    }

    @GetMapping("/test")
    @ResponseBody
    public String test() {
        return "My Spring MVC Works!";
    }

    // 1. 带查询参数的GET请求
    @GetMapping("/greet")
    @ResponseBody
    public String greet(
            @RequestParam(name = "name", defaultValue = "World") String name
    ) {
        return "Hello, " + name + "!";
    }

    // 2. 处理POST请求
    @PostMapping("/submit")
    @ResponseBody
    public String submit(@RequestParam("message") String message) {
        return "You submitted: " + message;
    }

    // 3. 使用路径变量
    @GetMapping("/user/{id}")
    @ResponseBody
    public String getUserById(@PathVariable String id) {
        return "User ID: " + id;
    }
}
